<?php

namespace app\admin\model\wechat;

use think\Model;


class Drafts extends Model
{

    // 表名
    protected $name = 'wechat_drafts';

    public function getDraftDetail($access_token, $media_id)
    {
        $url_material = "https://api.weixin.qq.com/cgi-bin/draft/get?access_token=" . $access_token;
        $media = '{
    "media_id":"' . $media_id . '"
}';
        $curl_info = curl_post($url_material, 'post', $media);


        $arr = json_decode($curl_info, true);
        $lists = $arr['news_item'];
        return $lists;
    }

    public function getDraftSelect($wechat_drafts, $detail)
    {
        $index = -1;
        foreach ($wechat_drafts as $k => $v) {
            if ($v['title'] === $detail['title'] && $v['author'] === $detail['author']) {
                $index = $k;
            }
        }

        if ($index === -1) {
            return array('index' => $index);
        } else {
            return array('index' => $index, 'detail' => $wechat_drafts[$index]);
        }
    }

    public function updateDraft($access_token, $media_id, $index, $detail, $thumb_media_id, $wechat_content)
    {

        $url_material = "https://api.weixin.qq.com/cgi-bin/draft/get?access_token=" . $access_token;
        $media = '{
    "media_id":"' . $media_id . '",
    "index":"' . $index . '",
    "articles":{
        "title":"' . $detail['title'] . '",
        "author":"' . $detail['author'] . '",
        "digest":"' . $detail['summary'] . '",
        "content":"' . $wechat_content . '",
        "content_source_url":"' . $detail['source_url'] . '",
        "thumb_media_id":"' . $thumb_media_id . '",
    }
}';
        $curl_info = curl_post($url_material, 'post', $media);


        $arr = json_decode($curl_info, true);

        return $arr;
    }

    public function deleteDraft($access_token, $media_id)
    {

        $url_material = "https://api.weixin.qq.com/cgi-bin/draft/delete?access_token=" . $access_token;
        $media = '{
    "media_id":"' . $media_id . '"
}';
        $curl_info = curl_post($url_material, 'post', $media);


        $arr = json_decode($curl_info, true);

        return $arr;
    }

}
